package com.fuja.fumo.ui;
import org.apache.log4j.Logger;

import com.fuja.fumo.handler.Criteria;
import com.fuja.fumo.handler.ExportHandler;
import com.fuja.fumo.model.Question;
import com.fuja.swf.ui.UIUtil;

import waba.fx.Color;
import waba.sys.Convert;
import waba.sys.Settings;
import waba.ui.*;
import waba.util.Vector;
public class UIExport extends Window{
	private static Logger log = Logger.getLogger(UIExport.class);
	private Vector questions;
	private ExportHandler handler = new ExportHandler();
	//Interface
	private Label lbInstructions = new Label("",LEFT);
	private Button btnWrong = new ExportButton("Export Wrong questions");
	private Button btnMarket = new ExportButton("Export Marked questions");
	private Button btnResume = new ExportButton("Export Resume");
	private Button btnBack = new Button("Back");
	public UIExport(Vector questions){
		super();		
		String aux = "Instructions: Mockito.html will be create in root folder of device with selected questions.";
		aux = Convert.insertLineBreak(Settings.screenWidth-10, '|', fm,aux);
		lbInstructions.setText(aux);
		log.debug("Create UIExport" );
		this.questions = questions;
	}
	public void onStart(){
		lbInstructions.setBackColor(Color.BLACK);
		lbInstructions.setForeColor(Color.WHITE);
		add(lbInstructions);
		lbInstructions.setRect(LEFT,TOP, Settings.screenWidth, PREFERRED);
		
		add(btnBack,RIGHT,AFTER + 1 );
		add(btnMarket);
		btnMarket.setRect(CENTER,CENTER,Settings.screenWidth-5, PREFERRED);
		add(btnWrong);
		btnWrong.setRect(CENTER,BEFORE -1,Settings.screenWidth-5, PREFERRED,btnMarket);
		add(btnResume);
		btnResume.setRect(CENTER,AFTER + 1,Settings.screenWidth-5, PREFERRED,btnMarket);
	}
	public void onEvent(Event e) {
		log.debug("onEvent type: " + e.type + ", targer: " + e.target.getClass().getName());
		
		boolean result = false;
		if ((e.target == btnBack) && (e.type==ControlEvent.PRESSED)){
			UIQuestion window = new UIQuestion(questions);
			UIUtil.swap(window);
		} else if ((e.target == btnResume) && (e.type==ControlEvent.PRESSED)){
			result = handler.process(questions);
		} else if ((e.target == btnWrong) && (e.type==ControlEvent.PRESSED)){
			result = handler.process(questions, new Criteria(){
				public boolean process(Question q){
					return q.isError();
				}
			});
		} else if ((e.target == btnMarket) && (e.type==ControlEvent.PRESSED)){
			result = handler.process(questions, new Criteria(){
				public boolean process(Question q){
					return q.isMarked();
				}
			});
		}
		if ((e.target instanceof ExportButton) && (e.type==ControlEvent.PRESSED)){
			if (result){
				UIUtil.showMsg("Result","HTML exported!");
			} else {
				String aux = "Error when generate the HTML file!";
				aux = Convert.insertLineBreak(Settings.screenWidth-10, '|', fm,aux);
				UIUtil.showMsg("Error",aux);				
			}
		}
		if (e.type==ControlEvent.PRESSED)
			repaintNow();
	}
	private class ExportButton extends Button{
		public ExportButton(String text) {
			super(text);
		}
	}
}
